/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package itproject.server.gui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.CardLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

/**
 *
 * @author tim
 */
public class ServerConfig {

    // Declare Swing Components
    JFrame configFrame;
    JMenuBar configJMenuBar;
    JMenu fileJMenu;
    JMenuItem exit;
    JPanel configPanel;
    JButton btnStart, btnStop;
    JLabel lblRmiHost, lblMySqlHost, lblDbUser, lblDbPass, lblDbName, lblServerLog;
    JTextField txfRmiHost, txfMySqlHost, txfDbUser, txfDbName;
    JPasswordField pwfDbPass;
    JTextArea txaServerLog;
    private JPanel buttonPanel;
    private JPanel contentPanel;

    // Main for testing purposes 
    public static void main(String args[]) {
        new ServerConfig();
    }

    public ServerConfig() {
        createAndShowGUI();
    }

    public void createAndShowGUI() {
        // Set LAF to System Standard LAF

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception exc) {
            System.err.println("Error loading L&F: " + exc);
        }

        // Initialising Swing Components
        configFrame = new JFrame("Easybooking Configuration");
        configFrame.setDefaultLookAndFeelDecorated(true);
        configFrame.setJMenuBar(this.createJMenuBar());
        configFrame.setSize(500, 400);
        configFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        configFrame.setContentPane(this.createContentPanel());
        configFrame.setResizable(false);
        configFrame.pack();
        configFrame.setVisible(true);

    }

    // Creates the MenuBar
    public JMenuBar createJMenuBar() {

        configJMenuBar = new JMenuBar();
        fileJMenu = new JMenu("File");
        exit = new JMenuItem("Exit");

        exit.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        fileJMenu.add(exit);
        configJMenuBar.add(fileJMenu);
        return configJMenuBar;
    }

    public JPanel createContentPanel() {
        contentPanel = new JPanel();

        // Sets the Layout of the contentPanel
        contentPanel.setLayout(new GridBagLayout());
        GridBagLayout gbl = new GridBagLayout();

        // Places the GUI elements in the layout using addComponent                                                         x, y, w, h, wx, wy
        addComponent(contentPanel, gbl, this.createConfigPanel(), GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 0, 1, 1, 0, 0);
        addComponent(contentPanel, gbl, this.createButtonPanel(), GridBagConstraints.NONE, GridBagConstraints.EAST, 0, 1, 1, 1, 0, 0);

        return contentPanel;
    }

    // Creates the Panel
    public JPanel createConfigPanel() {
        configPanel = new JPanel();

        // Sets the Border
        configPanel.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder("Easybooking Configuration"),
                BorderFactory.createEmptyBorder(20, 20, 20, 20)));


        configPanel.setLayout(new GridBagLayout());
        GridBagLayout gbl = new GridBagLayout();

        // Places the GUI elements in the layout using addComponent                                          x, y, w, h, wx, wy
        lblRmiHost = new JLabel("RMI-Host:");
        addComponent(configPanel, gbl, lblRmiHost, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 0, 1, 1, 0, 0);

        txfRmiHost = new JTextField(20);
        addComponent(configPanel, gbl, txfRmiHost, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 1, 0, 1, 1, 0, 0);

        lblMySqlHost = new JLabel("MYSQL-Host:");
        addComponent(configPanel, gbl, lblMySqlHost, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 1, 1, 1, 0, 0);

        txfMySqlHost = new JTextField(20);
        addComponent(configPanel, gbl, txfMySqlHost, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 1, 1, 1, 1, 0, 0);

        lblDbUser = new JLabel("DB-User:");
        addComponent(configPanel, gbl, lblDbUser, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 2, 1, 1, 0, 0);

        txfDbUser = new JTextField(20);
        addComponent(configPanel, gbl, txfDbUser, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 1, 2, 1, 1, 0, 0);

        lblDbPass = new JLabel("DB-Password:");
        addComponent(configPanel, gbl, lblDbPass, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 3, 1, 1, 0, 0);

        pwfDbPass = new JPasswordField(20);
        addComponent(configPanel, gbl, pwfDbPass, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 1, 3, 1, 1, 0, 0);

        lblDbName = new JLabel("DB-Name:");
        addComponent(configPanel, gbl, lblDbName, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 4, 1, 1, 0, 0);

        txfDbName = new JTextField(20);
        addComponent(configPanel, gbl, txfDbName, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 1, 4, 1, 1, 0, 0);

        lblServerLog = new JLabel("Server-Log:");
        addComponent(configPanel, gbl, lblServerLog, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 5, 1, 1, 0, 0);

        txaServerLog = new JTextArea(10, 40);
        addComponent(configPanel, gbl, txaServerLog, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0, 6, 2, 1, 0, 0);

        return configPanel;

    }

    public JPanel createButtonPanel() {
        buttonPanel = new JPanel();
        btnStart = new JButton("Start");
        btnStop = new JButton("Stop");

        // Sets the buttons the same size
        btnStop.setPreferredSize(btnStart.getPreferredSize());

        buttonPanel.add(btnStart);
        buttonPanel.add(btnStop);
        return buttonPanel;
    }

    // Adds the elements to the layout
    static void addComponent(Container cont, GridBagLayout gbl, Component c, int fill, int anchor, int x, int y, int width, int hight, double weightx, double weighty) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = fill;
        gbc.anchor = anchor;
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.gridwidth = width;
        gbc.gridheight = hight;
        gbc.weightx = weightx;
        gbc.weighty = weighty;
        gbc.insets = new Insets(10, 10, 10, 10);
        //gbl.setConstraints(c, gbc);
        cont.add(c, gbc);

    }
}
